/**
 * Tiny map class 
 */
T.Events.MAP_LOAD = 'mapload'; //地图载入开始
T.Events.MAP_LOADED = 'maploaded'; //地图载入完成

T.Map = T.Evented.extend({
    
    options: {
        projection: 'EPSG:3857'
        ,target: 'map'
    }
    
    /**
     * @param id {HTMLElement or String, Object} 地图容器
     * @param options {Object} 附加参数
     */
    ,initialize: function (id, options) {
        options = T.setOptions(this, options);
        
        this._initContainer(id,options);
        
        this._initPanes();
        
        this.tinyMap = new ol.Map(this.options);
        
        this._initEvents();
        
        this._layers = {}; //所有层
        
        if(this.options.layers)
            this.addLayer(this.options.layers);
    }
    
    // 公共方法
    
    /**
     * API: 改变地图视窗及缩放级别
     * @param center 中心点经纬度坐标
     * @param zoom 缩放级别
     */
    ,setView: function (center, zoom) {
        this.tinyMap.setView(new ol.View({
            projection: this.options.projection,
            center: ol.proj.transform(center, 'EPSG:4326', this.options.projection),
            zoom: zoom
        }));
        this._loaded = true;
        this.fire(T.Events.MAP_LOADED); //地图载入完成
        return this;
    }
    
    ,setZoom: function (zoom, options) {
    }

    ,zoomIn: function (delta, options) {
    }

    ,zoomOut: function (delta, options) {
    }
    
    ,getPane: function (pane) {
        return typeof pane === 'string' ? this._panes[pane] : pane;
    }
    
    ,createPane: function (name, container) {
        var className = 'ol-pane' + (name ? ' ol-' + name.replace('Pane', '') + '-pane' : ''),
            pane = T.DomUtil.create('div', className, container || this._mapPane);

        if (name) {
            this._panes[name] = pane;
        }
        return pane;
    }
    
    ,whenReady: function (callback, context) {
        if (this._loaded) {
            callback.call(context || this, {target: this});
        } else {
            this.on('load', callback, context);
        }
        return this;
    }
    
    // 私有方法
    
    /**
     * 初始化容器
     */
    ,_initContainer: function (id,options) {
        var container = this._container = 
            typeof id === 'string' ? document.getElementById(id) : id;

        if (!container) {
            throw new Error('地图容器没有找到. 目标:'+id);
        } else if (container._tiny) {
            throw new Error('地图已经被初始化!');
        }
        
        options.target = id;
        container._tiny = true;
    }
    
    ,_initPanes: function () {
        var panes = this._panes = {};

        this._mapPane = this._container;

        this.createPane('tilePane');
        this.createPane('shadowPane');
        this.createPane('overlayPane');
        this.createPane('markerPane');
        this.createPane('popupPane');
    }
    
    ,_initEvents: function () {
        var eventType = ['click', 'dblclick', 'moveend', 'pointerdrag', 'pointermove', 'singleclick'];
        for(var i in eventType){
            var et = eventType[i];
            var domEvent = T.bind(this._handleDOMEvent, this);
            this.tinyMap.on(et, domEvent);
        }
    }
    
    ,_handleDOMEvent: function(e){
        
        var type = e.type;
        
        if (!this.hasEventListeners(type)) { 
            return; 
        }

        if (type === 'contextmenu') {
            L.DomEvent.preventDefault(e);
        }
        
        var pixel = this.tinyMap.getEventPixel(e.originalEvent),
            latlng = e.coordinate,
            latlng4326 = ol.proj.transform(latlng, 'EPSG:3857', 'EPSG:4326');
        this.fire(type, {
            latlng: latlng,
            coords: latlng4326,
            pixel: pixel,
            originalEvent: e
        });
        
        var pixel = this.tinyMap.getEventCoordinate(e);
    }
});

T.map = function (id, options) {
    return new T.Map(id, options);
};
